package cn.edu.ahu.aircondition.modbus.service.serviceImpl;

import cn.edu.ahu.aircondition.modbus.service.domain.CtrlDomain;
import cn.edu.ahu.aircondition.modbus.service.domain.CtrlRegDomain;
import cn.edu.ahu.aircondition.modbus.service.domain.QueryRegDomain;
import cn.edu.ahu.aircondition.modbus.service.entity.*;
import cn.edu.ahu.aircondition.modbus.service.dao.*;
import cn.edu.ahu.aircondition.modbus.service.logic.AirConCtrlLogic;
import cn.edu.ahu.aircondition.modbus.service.service.AirConService;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * Created by ahu on 2017/6/11.
 */
public class AirConServiceImpl implements AirConService {
    private AirConCtrlDao airConCtrlDao;
    private AirConStatusDao airConStatusDao;
    private AirConStatusWaterDao airConStatusWaterDao;
    private AirConCtrlLogic airConCtrlLogic;
    private AirSetDao airSetDao;

    public AirConServiceImpl() {
        this.airConCtrlDao = new AirConCtrlDao();
        this.airConStatusDao = new AirConStatusDao();
        this.airConStatusWaterDao = new AirConStatusWaterDao();
        this.airConCtrlLogic = new AirConCtrlLogic();
        this.airSetDao = new AirSetDao();
    }

    @Transactional
    public AirConStatusEntity queryDataCompare(AirConStatusEntity oldAirConStatusEntity,QueryRegDomain newAirConStatusDomain){
        if (!oldAirConStatusEntity.getAoc().equals(newAirConStatusDomain.getAoc()) ||
                !oldAirConStatusEntity.getaTemp().equals(newAirConStatusDomain.getAirConCtrlTemp())||
                !oldAirConStatusEntity.getDwind().equals(newAirConStatusDomain.getWind())||
                !oldAirConStatusEntity.getfModel().equals(newAirConStatusDomain.getModel())
                ){
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            AirConStatusEntity newAirConStatusEntity = new AirConStatusEntity();
            newAirConStatusEntity.setDeviceAddress(newAirConStatusDomain.getDeviceAddress());
            newAirConStatusEntity.setAoc(newAirConStatusDomain.getAoc());
            newAirConStatusEntity.setaTemp(newAirConStatusDomain.getAirConCtrlTemp());
            newAirConStatusEntity.setDwind(newAirConStatusDomain.getWind());
            newAirConStatusEntity.setfModel(newAirConStatusDomain.getModel());
            newAirConStatusEntity.setErrMsg(newAirConStatusDomain.getErrMsg());
            newAirConStatusEntity.sethElec("无效");
            newAirConStatusEntity.setErrMsg(newAirConStatusDomain.getErrMsg());
            newAirConStatusEntity.seteTemp(newAirConStatusDomain.getEnvTemp());
            newAirConStatusEntity.setUpdateTime(String.valueOf(df.format(new Date())));
            if (!oldAirConStatusEntity.getAoc().equals(newAirConStatusDomain.getAoc())){
                newAirConStatusEntity.setAocTime(newAirConStatusEntity.getUpdateTime());
            }
            else {
                newAirConStatusEntity.setAocTime(oldAirConStatusEntity.getAocTime());
            }
            return newAirConStatusEntity;
        }

        else
            return null;
    }

    @Transactional
    public List<AirConSetEntity> listAirConSet() {
        return airSetDao.listAirConSet();
    }

    @Transactional
    public List<CtrlDomain> listCtrlDomain() {
        return airConCtrlLogic.listAirCtrlDomain();
    }

    @Transactional
    public List<CtrlRegDomain> listCtrlRegDomain() {
        List<CtrlDomain> ctrlDomainList =  airConCtrlLogic.listAirCtrlDomain();
        List<CtrlRegDomain> ctrlRegDomainList = new ArrayList<CtrlRegDomain>();
        for (CtrlDomain ctrlDomain:ctrlDomainList){
            CtrlRegDomain ctrlRegDomain = new CtrlRegDomain(ctrlDomain.getAirConSetEntity().getDeviceAddress());
            ctrlRegDomain.setDeviceId(ctrlDomain.getAirConSetEntity().getDeviceId());
            ctrlRegDomain.setAoc(ctrlDomain.getAirConCtrlEntity().getAoc());
            ctrlRegDomain.setModel(ctrlDomain.getAirConCtrlEntity().getfModel());
            ctrlRegDomain.setWind(ctrlDomain.getAirConCtrlEntity().getDwind());
            ctrlRegDomain.setTemp(ctrlDomain.getAirConCtrlEntity().getaTemp());
            ctrlRegDomainList.add(ctrlRegDomain);
        }
        return ctrlRegDomainList;
    }

    @Transactional
    public List<AirConStatusEntity> listAirConStatusDeviceAddress(List<Integer> deviceAddressList) {
        return airConStatusDao.listAirConStatusCurByDeviceAddress(deviceAddressList);
    }

    @Transactional
    public List<AirConCtrlEntity> listAirConCtrlByDeviceId(List<Integer> deviceIdList) {
        if (deviceIdList != null)
            return airConCtrlDao.listAirConStatusCurByDeviceId(deviceIdList);
        else
            return null;
    }

    @Transactional
    public void saveAirStatusWater(List<AirConStatuWaterEntity> airConStatuWaterEntityList) {
        if (airConStatuWaterEntityList != null)
            airConStatusWaterDao.saveAirConStatusCur(airConStatuWaterEntityList);
    }

    @Transactional
    public void updateAirConStatus(List<AirConStatusEntity> airConStatusEntityList) {
        if(airConStatusEntityList!= null)
            airConStatusDao.updateAirConStatusCur(airConStatusEntityList);
    }

    @Transactional
    public void updateAirConCtrl(List<AirConCtrlEntity> airConCtrlEntityList) {
        if(airConCtrlEntityList!= null)
            airConCtrlDao.updateAirConStatusCur(airConCtrlEntityList);
    }

    public void setAirConStatusWaterDao(AirConStatusWaterDao airConStatusWaterDao) {
        this.airConStatusWaterDao = airConStatusWaterDao;
    }

    public void setAirConStatusDao(AirConStatusDao airConStatusDao) {
        this.airConStatusDao = airConStatusDao;
    }

    public void setAirConCtrlDao(AirConCtrlDao airConCtrlDao) {
        this.airConCtrlDao = airConCtrlDao;
    }
}
